//
//  UpgradeViewController.m
//  DA_V
//
//  Created by 周季阳 on 15/4/29.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "UpgradeViewController.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "OffLineView.h"
#import "OffLineTableViewCell.h"
#import "ZUpgradeViewController.h"
#import "OfflineListViewController.h"
#import "YoungAllData.h"
@interface UpgradeViewController ()<UITableViewDataSource,UITableViewDelegate,YoungDelegate>
{
    UITableView *zTableView;
    OffLineView *offView;
}
@property(nonatomic,retain)NSMutableArray *zArrayData;
@end

@implementation UpgradeViewController
@synthesize zArrayData;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    [self initNav];
    navTitle.text = @"升级审核";
    
    
    UIButton *belowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    belowBtn.frame = CGRectMake(0,SCREEN_HEIGHT-(0.15625*SCREEN_WIDTH) , SCREEN_WIDTH, 0.15625*SCREEN_WIDTH);
    [belowBtn setBackgroundImage:[UIImage imageNamed:@"升级申请-列表_02"] forState:UIControlStateNormal];
    [belowBtn addTarget:self action:@selector(backGo) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:belowBtn];
    
    YoungAllData *data = [[YoungAllData alloc]init];
    data.delegate = self;
    NSLog(@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"userID"]);
    [data gainUpgrade:[[NSUserDefaults standardUserDefaults]objectForKey:@"userID"]];
    
}
#pragma mark - 获取升级申请回调
-(void)upgrade:(NSDictionary *)sign{
    if ([[sign objectForKey:@"status"] isEqualToString:@"success"]) {
    zArrayData = [[NSMutableArray alloc]initWithArray:[sign objectForKey:@"data"]];
    [self initUI];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
#pragma mark - 获取升级申请回调失败
-(void)requistFail{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
#pragma 返回
-(void)clickBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)backGo{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - initUI
-(void)initUI{
    
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, navView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT*0.6)];
    backgroundView.backgroundColor = [UIColor whiteColor];
    backgroundView.userInteractionEnabled = YES;
    [self.view addSubview:backgroundView];
    
    
    offView = [[OffLineView alloc] initWithFrame:CGRectMake(10, 30, SCREEN_WIDTH-20, 40)];
    offView.backgroundColor = [UIColor whiteColor];
    offView.serialNumber.text = @"姓名";
    offView.serialNumber.font = [UIFont systemFontOfSize:14];
    offView.theAgent.text = @"电话";
    offView.theAgent.font = [UIFont systemFontOfSize:14];
    offView.phoneNumber.text = @"状态";
    offView.phoneNumber.font = [UIFont systemFontOfSize:14];
    offView.orderAmount.text = @"销售额";
    offView.orderAmount.font = [UIFont systemFontOfSize:14];
    [backgroundView addSubview:offView];
    
    
    zTableView = [[UITableView alloc] initWithFrame:CGRectMake(10,offView.bottom, self.view.frame.size.width-19,180)];
    zTableView.delegate = self;
    zTableView.dataSource = self;
    [backgroundView addSubview:zTableView];
    zTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    zTableView.backgroundColor = [UIColor clearColor];
    [zTableView registerClass:[OffLineTableViewCell class] forCellReuseIdentifier:@"cell"];
    zTableView.scrollEnabled = NO;
    [self setExtraCellLineHidden:zTableView];
    
    UIButton *pagesBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    if (SCREEN_WIDTH <500) {
        pagesBtn.frame = CGRectMake(self.view.frame.size.width-70,zTableView.bottom+10, 64, 25) ;
    }else{
        pagesBtn.frame = CGRectMake(self.view.frame.size.width-70,zTableView.bottom+10, 64, 25) ;
    }
    pagesBtn.tag = 29;
    [pagesBtn setBackgroundImage:[UIImage imageNamed:@"selectBtn"] forState:UIControlStateNormal];
    [pagesBtn setTintColor:[UIColor blackColor]];
    [pagesBtn setTitle:@"第1页" forState:UIControlStateNormal];
    pagesBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [pagesBtn addTarget:self action:@selector(pageBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [pagesBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -20, 0, 0)];
    pagesBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
    [backgroundView addSubview:pagesBtn];
    
    
}
#pragma mark - clickBtn
-(void)pageBtnAction:(UIButton *)sender{
    NSLog(@"");
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    //    if (self.arr.count<t*6) {
    if (1) {
        if (SCREEN_HEIGHT<500) {
            //return self.arr.count-(t-1)*5;
            return 5;
        }else{
            //return self.arr.count-(t-1)*6;
            return [zArrayData count];
        }
    }else{
        if (SCREEN_HEIGHT<500) {
            return 5;
        }else{
            return 6;
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 30;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    OffLineTableViewCell *cell = [[OffLineTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    
    cell.serialNumber.text =[NSString stringWithFormat:@"%@",[[zArrayData objectAtIndex:indexPath.row] objectForKey:@"RealName"]];
    cell.theAgent.text = [NSString stringWithFormat:@"%@",[[zArrayData objectAtIndex:indexPath.row] objectForKey:@"MobileNumber"]];
    cell.phoneNumber.text = [NSString stringWithFormat:@"%@",[[zArrayData objectAtIndex:indexPath.row] objectForKey:@"AgentStatus"]];
    cell.orderAmount.text = [NSString stringWithFormat:@"%@",[[zArrayData objectAtIndex:indexPath.row] objectForKey:@"SellAmount"]];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    OfflineListViewController *offllinelist = [[OfflineListViewController alloc] init];
    offllinelist.AgentID = [[zArrayData objectAtIndex:indexPath.row] objectForKey:@"AgentID"];
    [self.navigationController pushViewController:offllinelist animated:YES];
}
#pragma mark - 隐藏多余的分割线
-(void)setExtraCellLineHidden: (UITableView *)tableView

{
    UIView *view = [UIView new];
    
    view.backgroundColor = [UIColor clearColor];
    
    [tableView setTableFooterView:view];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
